<template>
	<view class="banner-view">
		<u-swiper class="banner"
			height="200" 
			name="cover" 
			:list="bannerList" 
			border-radius="30"
			@click="itemClick">
		</u-swiper>
	</view>
</template>

<script>
	import { getBanner } from '@/api/common.js'
	import { getTicket } from '@/utils/auth.js'
	export default {
		name:"BannerView",
		props: {
			position: {
				type: String,
				default () {
					return '0'
				}
			},
			height: {
				type: Number,
				default () {
					return 200
				}
			}
		},
		data() {
			return {
				bannerList: [],
			};
		},
		computed: {
			channel() {
				return getApp().globalData.channel;
			},
		},
		created() {
			const ticket = getTicket();
			if (!this.$u.test.isEmpty(ticket)) {
				this.getBannerList();
			};
		},
		methods: {
			getBannerList() {
				getBanner(this.position).then(res => {
					console.log(res);
					this.bannerList = res.data.content;
					if (this.bannerList.length === 0) {
						if (this.channel === 'xm') {
							this.bannerList.push({cover: 'https://dotflat-saas-res.oss-cn-shanghai.aliyuncs.com/xybaby365/app/icons/banner_default_xm.png'})
						} else if (this.channel === 'xybaby') {
							this.bannerList.push({cover: 'https://dotflat-saas-res.oss-cn-shanghai.aliyuncs.com/xybaby365/app/icons/zd/banner.png'})
						} else if (this.channel === 'jzwy') {
							this.bannerList.push({cover: 'https://dotflat-saas-res.oss-cn-shanghai.aliyuncs.com/xybaby365/app/icons/banner_jzwy.png'})
						} else {
							this.bannerList.push({cover: 'https://dotflat-saas-res.oss-cn-shanghai.aliyuncs.com/xybaby365/app/icons/banner_default.png'})
						}
					}
				}).catch(err => {
					console.log(err);
				})
			},
			
			itemClick(index) {
				const item = this.bannerList[index];	
				console.log(item);
				if (item.link.indexOf('http') === 0) {
					this.$u.route({
						url: 'pages_base/common/webview',
						params: {
							url: item.link
						}
					})
				} else if (item.link.indexOf('xybaby365://') === 0) {
					console.log(item.link);
					const scheme = item.link.substring(12);
					console.log(scheme);
					uni.navigateTo({
						url: `/${scheme}`
					})
				} 
			}
		}
	}
</script>

<style lang="scss">
	.banner-view{
		display: flex;
		justify-content: center;
		align-items: center;
		.banner{
			width: 690rpx;
			height: 200rpx;
		}
	}
</style>
